package com.ruitu.mall_98_2.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyRefreshBaseActivity;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.MyAdvanceOrderContract;
import com.ruitu.mall_98_2.mvp.model.MyAdvanceOrderModel;
import com.ruitu.mall_98_2.mvp.presenter.MyAdvanceOrderPresenter;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.order.ConfirOrder;
import com.ruitu.router_module.bean.order.MyAdvanceOrder;
import com.ruitu.router_module.bean.order.SmallOrder;
import com.ruitu.router_module.bean.product.Product;

import java.util.ArrayList;
import java.util.List;

public class MyAdvanceOrderActivity extends MyRefreshBaseActivity<MyAdvanceOrderPresenter, MyAdvanceOrderModel> implements MyAdvanceOrderContract.View {

    private DataAdapter adapter;
    private List<Integer> skipId = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHeadTitle("我的预订");
        setLoadMoreEnabled(false);
        skipId.add(R.id.header);
    }

    @Override
    protected void reqData() {
        reReq();
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lRecyclerView);
    }

    @Override
    protected void reqFirst() {
        reReq();
    }

    @Override
    protected void reqMore() {

    }

    @Override
    protected void reReq() {
        showProgress();
        mPresenter.getMinePreOrder(AppHolder.instance().getUser().getAccountId());
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_advance_order;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return findViewById(R.id.progress);
    }

    @Override
    public void setData(List<MyAdvanceOrder> myAdvanceOrders) {
        refreshComplete();
        if (isDataEmpty(myAdvanceOrders)) {
            showEmptyView(skipId);

        } else {
            adapter.setDataList(myAdvanceOrders);
            showContent(null);
        }
    }

    @Override
    public void setFail() {
        refreshComplete();
        showError(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reReq();
            }
        });
    }

    class DataAdapter extends ListBaseAdapter<MyAdvanceOrder> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_my_advance_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            TextView tv_company_name = holder.getView(R.id.tv_company_name);
            ImageView iv_company_logo = holder.getView(R.id.iv_company_logo);
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_info = holder.getView(R.id.tv_info);
            TextView tv_send_message = holder.getView(R.id.tv_send_message);
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tv_order_detail = holder.getView(R.id.tv_order_detail);
            TextView tv_pay_order_money = holder.getView(R.id.tv_pay_order_money);
            TextView tv_pay_balance_payment = holder.getView(R.id.tv_pay_balance_payment);
            TextView tv_num = holder.getView(R.id.tv_num);
            TextView tv_edite = holder.getView(R.id.tv_edite);
            TextView tv_order_status = holder.getView(R.id.tv_order_status);
            TextView tv_need_pay = holder.getView(R.id.tv_need_pay);
            TextView tv_pay_advance_money = holder.getView(R.id.tv_pay_advance_money);
            TextView tv_end_pay = holder.getView(R.id.tv_end_pay);
            TextView tv = holder.getView(R.id.tv);
            LinearLayout ll_pay = holder.getView(R.id.ll_pay);
            LinearLayout ll_item = holder.getView(R.id.ll_item);
            final MyAdvanceOrder myAdvanceOrder = mDataList.get(position);
            tv_company_name.setText(myAdvanceOrder.getCompany().getName());
            tv_commodity_name.setText(myAdvanceOrder.getGoodName());
            Glide.with(mContext).load(Config.BASE_URL + myAdvanceOrder.getGoodsUrl()).apply(Arad.opts).into(iv_commodity);
            Glide.with(mContext).load(Config.BASE_URL + myAdvanceOrder.getCompany().getCompanyUrl()).apply(Arad.opts_shop).into(iv_company_logo);
            tv_info.setText(myAdvanceOrder.getGoodsSpec());
            tv_num.setText("x" + myAdvanceOrder.getGoodsNum());
            if (myAdvanceOrder.getStatus().equals("2")) {
                tv_price.setText("￥" + myAdvanceOrder.getGoodsPrice());
                tv.setVisibility(View.GONE);
                if (myAdvanceOrder.getOrder() != null) {
                    if (myAdvanceOrder.getOrder().getIsPayOver().equals("2")) {
//                        tv_order_detail.setVisibility(View.VISIBLE);
                        tv_pay_order_money.setVisibility(View.GONE);
                        tv_edite.setVisibility(View.GONE);
                        tv_pay_balance_payment.setVisibility(View.GONE);
                        tv_order_status.setText("卖家制作中");
                        tv_need_pay.setText("已付订金:" + myAdvanceOrder.getOrder().getAdvanceMoney() + "元");
                        tv_pay_advance_money.setText("已付订金:" + myAdvanceOrder.getOrder().getAdvanceMoney() + "元");
                        tv_end_pay.setText("需付尾款:" + myAdvanceOrder.getOrder().getTailMoney() + "元");
                        tv_send_message.setVisibility(View.VISIBLE);
                        ll_pay.setVisibility(View.VISIBLE);
                    } else if (myAdvanceOrder.getOrder().getIsPayOver().equals("0")) {
                        tv_order_detail.setVisibility(View.GONE);
                        tv_pay_order_money.setVisibility(View.VISIBLE);
                        tv_edite.setVisibility(View.GONE);
                        tv_pay_balance_payment.setVisibility(View.GONE);
                        tv_order_status.setText("卖家已确认");
                        tv_need_pay.setText("需付订金:" + myAdvanceOrder.getAdvanceRatio() + "%");
                        tv_pay_advance_money.setText("需付订金:" + myAdvanceOrder.getOrder().getAdvanceMoney() + "元");
                        tv_end_pay.setText("尾款:" + myAdvanceOrder.getOrder().getTailMoney() + "元");
                        ll_pay.setVisibility(View.VISIBLE);
                        tv_send_message.setVisibility(View.GONE);
                    } else if (myAdvanceOrder.getOrder().getIsPayOver().equals("4")) {
                        tv_order_detail.setVisibility(View.GONE);
                        tv_pay_order_money.setVisibility(View.GONE);
                        tv_edite.setVisibility(View.GONE);
                        tv_pay_balance_payment.setVisibility(View.VISIBLE);
                        tv_pay_advance_money.setText("已付订金:" + myAdvanceOrder.getOrder().getAdvanceMoney() + "元");
                        tv_end_pay.setText("需付尾款" + myAdvanceOrder.getOrder().getTailMoney() + "元");
                        tv_need_pay.setText("需付尾款:" + myAdvanceOrder.getOrder().getTailMoney() + "元");
                        tv_order_status.setText("等待买家支付尾款");
                        ll_pay.setVisibility(View.VISIBLE);
                        tv_send_message.setVisibility(View.GONE);
                    } else if (myAdvanceOrder.getOrder().getIsPayOver().equals("3")) {
                        tv_order_detail.setVisibility(View.GONE);
                        tv_pay_order_money.setVisibility(View.GONE);
                        tv_edite.setVisibility(View.GONE);
                        tv_pay_balance_payment.setVisibility(View.GONE);
                        tv_order_status.setText("买家已取消交易");
                        ll_pay.setVisibility(View.GONE);
                        tv_send_message.setVisibility(View.GONE);
                    } else if (myAdvanceOrder.getOrder().getIsPayOver().equals("1")) {
//                        tv_order_detail.setVisibility(View.VISIBLE);
                        tv_pay_order_money.setVisibility(View.GONE);
                        tv_edite.setVisibility(View.GONE);
                        tv_pay_balance_payment.setVisibility(View.GONE);
                        tv_order_status.setText("买家已付款");
                        ll_pay.setVisibility(View.VISIBLE);
                        tv_pay_advance_money.setText("已付订金:" + myAdvanceOrder.getOrder().getAdvanceMoney() + "元");
                        tv_end_pay.setText("已付尾款" + myAdvanceOrder.getOrder().getTailMoney() + "元");
                        tv_send_message.setVisibility(View.GONE);
                    }
                }
            } else if (myAdvanceOrder.getStatus().equals("0") || myAdvanceOrder.getStatus().equals("1")) {
                tv.setVisibility(View.VISIBLE);
                tv_order_detail.setVisibility(View.GONE);
                tv_pay_order_money.setVisibility(View.GONE);
                tv_edite.setVisibility(View.VISIBLE);
                tv_pay_balance_payment.setVisibility(View.GONE);
                ll_pay.setVisibility(View.GONE);
                tv_order_status.setText("待确认");
                tv_send_message.setVisibility(View.GONE);
            } else {
                tv_order_detail.setVisibility(View.GONE);
                tv_pay_order_money.setVisibility(View.GONE);
                tv_edite.setVisibility(View.GONE);
                tv_pay_balance_payment.setVisibility(View.GONE);
                ll_pay.setVisibility(View.GONE);
                tv.setVisibility(View.GONE);
                tv_order_status.setText("卖家已拒绝");
                tv_send_message.setVisibility(View.GONE);
            }

            tv_edite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("preOrder", myAdvanceOrder);
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.BookProActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }

                }
            });

            tv_order_detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SmallOrder smallOrder = new SmallOrder();
                    smallOrder.setId(myAdvanceOrder.getOrder().getOrderDetails().get(0).getId());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("smallOrder", smallOrder);
                    startActivity(OrderDetailsActivity.class, bundle);
                }
            });

            tv_pay_order_money.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ConfirOrder confirOrder = new ConfirOrder();
                    confirOrder.setId(myAdvanceOrder.getOrder().getId());
                    confirOrder.setIsPayOver(myAdvanceOrder.getOrder().getIsPayOver());
                    confirOrder.setOrderType(myAdvanceOrder.getOrder().getOrderType());
                    confirOrder.setConsigneeName(myAdvanceOrder.getOrder().getConsigneeName());
                    confirOrder.setCollectGoodsPhone(myAdvanceOrder.getOrder().getCollectGoodsPhone());
                    confirOrder.setOrderProvinceCode(myAdvanceOrder.getOrder().getOrderProvinceCode());
                    confirOrder.setOrderCityCode(myAdvanceOrder.getOrder().getOrderCityCode());
                    confirOrder.setOrderCountyCode(myAdvanceOrder.getOrder().getOrderCountyCode());
                    confirOrder.setCollectGoodsAddress(myAdvanceOrder.getOrder().getCollectGoodsAddress());
                    Intent intent = new Intent(MyAdvanceOrderActivity.this, PayOrderActivity.class);
                    intent.putExtra("confirOrder", confirOrder);
                    startActivity(intent);
                }
            });

            tv_pay_balance_payment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ConfirOrder confirOrder = new ConfirOrder();
                    confirOrder.setId(myAdvanceOrder.getOrder().getId());
                    confirOrder.setIsPayOver(myAdvanceOrder.getOrder().getIsPayOver());
                    confirOrder.setOrderType(myAdvanceOrder.getOrder().getOrderType());
                    confirOrder.setConsigneeName(myAdvanceOrder.getOrder().getConsigneeName());
                    confirOrder.setCollectGoodsPhone(myAdvanceOrder.getOrder().getCollectGoodsPhone());
                    confirOrder.setOrderProvinceCode(myAdvanceOrder.getOrder().getOrderProvinceCode());
                    confirOrder.setOrderCityCode(myAdvanceOrder.getOrder().getOrderCityCode());
                    confirOrder.setOrderCountyCode(myAdvanceOrder.getOrder().getOrderCountyCode());
                    confirOrder.setCollectGoodsAddress(myAdvanceOrder.getOrder().getCollectGoodsAddress());
                    Intent intent = new Intent(MyAdvanceOrderActivity.this, PayOrderActivity.class);
                    intent.putExtra("confirOrder", confirOrder);
                    startActivity(intent);
                }
            });
            final Bundle bundle = new Bundle();
            ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myAdvanceOrder.getStatus().equals("2") && myAdvanceOrder.getOrder() != null) {
                        if (myAdvanceOrder.getOrder().getIsPayOver().equals("1")) {
                            SmallOrder smallOrder = new SmallOrder();
                            smallOrder.setId(myAdvanceOrder.getOrder().getOrderDetails().get(0).getId());
                            bundle.putSerializable("smallOrder", smallOrder);
                            startActivity(OrderDetailsActivity.class, bundle);
                        } else {
                            Intent intent = new Intent(MyAdvanceOrderActivity.this, WaitingPayOrderDetailsActivity.class);
                            intent.putExtra("orderList", myAdvanceOrder.getOrder());
                            startActivity(intent);
                        }
                    } else {

                        Product product = new Product();
                        product.setGoodId(myAdvanceOrder.getGoodsId());
                        bundle.putSerializable("product", product);
                        try {
                            A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            tv_company_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        bundle.putString("companyId", myAdvanceOrder.getCompany().getId());
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ShopDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });


        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        reReq();
    }
}
